import Taro ,{ Component } from '@tarojs/taro'
import { View , Text} from '@tarojs/components'

import { AtSwipeAction } from "taro-ui"

import './receiveAddressList.scss'

import ListEmptyView from '../../component/LiseEmptyView/listEmptyView.js'
import emptyIcon from '../../image/ic_empty.png'
import BasePage from "../basePage";
import AddressItem from '../../component/AddressItem/addressItem.js'
import {base_url} from "../../network/config";
import DataManager from "../../utils/DataManager";

export default class receiveAddressList extends BasePage{

  config = {
    navigationBarTitleText: '我的收货地址'
  }

  constructor() {
    super(...arguments)
    this.state = {
      addressList: [],
    }
  }

  onPullDownRefresh(){
    Taro.stopPullDownRefresh();
  }

  componentWillMount(){
  }

  componentDidShow(){
    this.loadAddressList()
  }

  loadAddressList(){
    let _this = this;
    let url = base_url + 'rest/user/address/addressList'
    Taro.request({
      url:url,
      data: {
        token:DataManager.getToken(),
        platform:'android_c',
        weixinId:DataManager.getWeiXinId(),
        loginChannel:'c',
        appId:'KC876D98B30UA40CJSNHI689FB33SDW',
        digest : 'dda484557c12febe48139d8f44051c13',
      },
      method: 'GET',
      success(res) {
        if (res.data && res.data.data){
          _this.setState({
            addressList: res.data.data,
          })
        }
      },
      fail(){
      }
    })
  }

  deleteAddress(addressId){


    Taro.request({
      url: 'https://www.vpin.top/a/rest/user/address/deleteAddress',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      dataType: 'json',
      data:{
        token:DataManager.getToken(),
        platform:'android_c',
        weixinId:DataManager.getWeiXinId(),
        loginChannel:'wechat',
        appId:'KC876D98B30UA40CJSNHI689FB33SDW',
        digest : 'dda484557c12febe48139d8f44051c13',
        addressId:addressId
      },
      success: (obj) => {
        if (obj.data.status == 'success') {
          Taro.showToast({
            title: '删除成功',
            icon: 'success',
            duration: 1000
          });
          this.loadAddressList()
        } else {
          Taro.showToast({
            title: '删除失败',
            icon: 'none',
            duration: 1000
          })
        }
      },
      fail: (obj) => {
        Taro.showToast({
          title: '程序异常',
          icon: 'none',
          duration: 1000
        })
      }
    })
  }

  toAddAddress = () =>{
    Taro.navigateTo({
      url : `/pages/me/addReceiveAddress`
    })
  }

  handleClick = (item , data) =>{
    if (data.text == '删除'){
      this.deleteAddress(item.id);
    }
  }

  render(){

    let options = [{ text: '删除', style: { backgroundColor: '#FF4949' } }];

    return(
      <View className='receive-address-list'>
        <View className='receive-address-list-sub'>
          {this.state.addressList.length > 0 ? this.state.addressList.map(addressItem => {
              return (
                <AtSwipeAction autoClose  onClick={this.handleClick.bind(this , addressItem)} options={options}>
                  <AddressItem addressData={addressItem}></AddressItem>
                </AtSwipeAction>
              )
            })
            :
            <ListEmptyView emptyText='暂无数据' emptyImage={emptyIcon}/>
          }
        </View>
        <View className='address-list-add' onClick={this.toAddAddress.bind(this)}>
          <Text className='address-list-add-text'>添加收货地址</Text>
        </View>
      </View>
    )
  }


}




